import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Authors - Oren Bracha, Rony Lahav & Yehuda Eterman
 * This class 
 *
 */
public class DateStringConverter {
	/**
	 * @return current time in logger format
	 */
	public static synchronized String timeCurrentForLogger() {
		String date = formatForLogger.format(new Date(System.currentTimeMillis()));
		return date.substring(0, 11);
	}
	
	/**
	 * @return current time in message format
	 */
	public static synchronized String timeCurrentForMessages() {
		return messageTimeFormat.format(new Date(System.currentTimeMillis()));
	}

	/**
	 * compare 2 dates
	 * @param first - first date
	 * @param second - second date
	 * @return - 	0 if equal, 
	 * 				1 if first bigger, 
	 * 				-1 if second bigger
	 */
	public static int compare2Dates(String first, String second){
		long firstTime = getMSFromStringMessageFormat(first);
		long secondTime = getMSFromStringMessageFormat(second);
		//check which is older
		if (firstTime > secondTime) {
			return 1;
		}else if(firstTime == secondTime){
			return 0;
		}else{
			return -1;
		}
	}
	
	/**
	 * get string time by message format
	 * @param time
	 * @return
	 */
	public static synchronized String getMessageFormatFromMS(long time)
	{
		return messageTimeFormat.format(new Date(time));
	}
	
	/**
	 * @return message earliest possible time
	 */
	public static synchronized String getZeroDateAsString(){
		return messageTimeFormat.format(new Date(0L));
	}

	/**
	 * get MS time by message format string
	 */
	public static synchronized long getMSFromStringMessageFormat(String messageTime)
	{
		try {
			return messageTimeFormat.parse(messageTime).getTime();
		} catch (ParseException e) {
			return 0;
		}
	}

	//2 formats for the project
	private static SimpleDateFormat formatForLogger = new SimpleDateFormat("HH:mm:ss.SS");
	private static SimpleDateFormat messageTimeFormat = new SimpleDateFormat("dd/MM/yy;HH:mm:ss");
}
